package jzbank.util;


import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

/**
 * 加载配置文件，常驻内存
 * @author lzn
 * 2012-8-16 15:18:02
 *
 */
@Slf4j
public class ConfigReader{
	
	private static ConfigReader configReader=null;
	
	private  Properties pro = new Properties();


	private String propertiesPath = "";
	
	
	public Properties getPro() {
		return pro;
	}

	public void setPro(Properties pro) {
		this.pro = pro;
	}
	
	public String getValue(String key)
	{
		if(this.getPro().getProperty(key) != null && this.getPro().getProperty(key) != ""){
			return this.getPro().getProperty(key).trim();
		}else {
			return "";
		}
	}
	public void setValue(String key, String value){
        try {
            this.getPro().setProperty(key,value);
            HashMap<String, Object> keyValues = new HashMap<String, Object>();
            keyValues.put(key, value);
            setValueEx(keyValues);
        } catch (Exception e) {
            e.printStackTrace();
            log.debug("更改配置文件参数失败:|" + e);
        }
    }

    public void setValueEx(Map<String, Object> map) {
        try {
            InputStream in = new FileInputStream(new File(propertiesPath));
            BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
            StringBuffer outstr = new StringBuffer("");
            String line = "";
            boolean flag = false;
            while ((line = br.readLine()) != null) {
                if (line == "") // 如果为空
                {
                    outstr.append("\n");
                } else {
                    if (line.startsWith("#")) // 如果为注释
                    {
                        outstr.append(line + "\n");
                    } else {
                        String _line = line.trim();
                        int charNum = _line.indexOf("=");
                        // 判断是否是key/value格式
                        if (charNum != -1) {
                            Iterator<String> it = map.keySet().iterator();
                            boolean find = false;
                            while (it.hasNext()) {
                                String key = it.next();
                                String _key = _line.substring(0, charNum);
                                // 如果key相同，则更新value
                                if (_key.equals(key)) {
                                    String _value = map.get(key) + "";
                                    outstr.append(_key + "=" + _value + "\n");
                                    map.remove(key);
                                    flag = true;
                                    find = true;
                                    break;
                                }
                            }
                            if (!find) {
                                outstr.append(line + "\n");
                            }
                        } else {
                            outstr.append(line + "\n");
                        }
                    }
                }
            }
            // System.out.println(outstr);
            br.close();
            in.close();
            if (flag) {
                OutputStreamWriter fos = new OutputStreamWriter(new FileOutputStream(propertiesPath), "UTF-8");
                BufferedWriter bw = new BufferedWriter(fos);
                bw.write(outstr.toString());
                bw.close();
                fos.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.debug("更改配置文件参数到文件失败:|" + e);
        }
    }

	public ConfigReader(String URI){
		try {
			InputStream  in = new FileInputStream(URI);
			pro.load(in);
            this.propertiesPath=URI;
        } catch (FileNotFoundException e) {
			e.printStackTrace();
			log.error("读取"+URI+"文件时，出现异常"+e.getMessage());
		} catch (IOException e) {
			e.printStackTrace();
			log.error("读取"+URI+"文件时，出现异常"+e.getMessage());
		}
	}
	
	public static ConfigReader getInstance(String URI){
		if(configReader==null){
			configReader = new ConfigReader(URI);
		} 
		return configReader;
	}
}